package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.*;
import com.xmy.cultivate.service.ISkillItemsSetService;
import com.xmy.cultivate.service.ISkillShutnumService;
import com.xmy.cultivate.service.ISkillStationsItemsService;
import com.xmy.cultivate.service.ISkillStationsService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 培训项目设置 前端控制器
 * </p>
 *
 * @author hpiggy
 * @since 2025-08-06
 */
@RestController
@RequestMapping("/api/skillItemsSet")
public class SkillItemsSetController {

    @Autowired
    ISkillItemsSetService iSkillItemsSetService;
    @Autowired
    ISkillShutnumService iSkillShutnumService;
    @Autowired
    ISkillStationsItemsService iSkillStationsItemsService;

    @GetMapping("/findAll")
    public Result findAll(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                          @RequestParam(value = "departmentId", required = false, defaultValue = "27") String departmentId,  //27：市场培训，23：教学培训
                          @RequestParam(value = "status", required = false) String status,
                          @RequestParam(value = "type", required = false, defaultValue = "1") String type
    ) {
        QueryWrapper<SkillItemsSet> queryWrapper = new QueryWrapper<>();
        queryWrapper.inSql(StringUtils.isNotBlank(type), "type", type);
        queryWrapper.eq(StringUtils.isNotBlank(departmentId), "department_id", departmentId);
//        queryWrapper.eq("status", 1);
        queryWrapper.orderByAsc("sort");
        List<SkillItemsSet> skillItemsSetList = iSkillItemsSetService.list(queryWrapper);
        return new Result(Code.OK, skillItemsSetList, "查询成功");
    }


    @PostMapping("/addSkillItemsSet")
    public Result addSkillItemsSet(@RequestBody SkillItemsSet skillItemsSet) {
        if (skillItemsSet.getType() == null) {
            skillItemsSet.setType(1);
        }
        if (skillItemsSet.getDepartmentId() == null) {
            skillItemsSet.setDepartmentId(27L);
        }
        Long count = iSkillItemsSetService.count(new QueryWrapper<>(skillItemsSet).eq("name", skillItemsSet.getName().trim()));
        if (count > 0) {
            return new Result(Code.ERROR, null, "添加失败，已存在该项目，不要重复添加");
        } else {
            boolean save = iSkillItemsSetService.save(skillItemsSet);
            if (!save) {
                return new Result(Code.ERROR, null, "添加失败");
            }
        }

        return new Result(Code.OK, null, "添加成功");
    }

    @PutMapping("/editSkillItemsSet")
    public Result editSkillItemsSet(@RequestBody SkillItemsSet skillItemsSet) {
        SkillItemsSet skillItemsSetOld = iSkillItemsSetService.getById(skillItemsSet.getId());
        if (Objects.isNull(skillItemsSetOld)) {
            return new Result(Code.ERROR, null, "该数据不存在");
        }
        skillItemsSetOld.setName(skillItemsSet.getName());
        skillItemsSetOld.setStatus(skillItemsSet.getStatus());
        boolean update = iSkillItemsSetService.updateById(skillItemsSetOld);
        if (!update) {
            return new Result(Code.ERROR, null, "修改失败");
        }
        return new Result(Code.OK, null, "修改成功");
    }

    @DeleteMapping("/deleteSkillItemsSet")
    public Result deleteSkillItemsSet(@RequestBody SkillItemsSet skillItemsSet) {
        SkillItemsSet skillItemsSetOld = iSkillItemsSetService.getById(skillItemsSet.getId());
        if (Objects.isNull(skillItemsSetOld)) {
            return new Result(Code.ERROR, null, "该数据不存在");
        }
        QueryWrapper<SkillShutnum> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("items_id", skillItemsSet.getId());
        queryWrapper.eq("deleted", 0);
        long count = iSkillShutnumService.count(queryWrapper);
        if (count > 0) {
            return new Result(Code.ERROR, null, "该数据有绑定数据,无法删除");
        }
        boolean delete = iSkillItemsSetService.removeById(skillItemsSet);
        if (!delete) {
            return new Result(Code.ERROR, null, "删除失败");
        }
        return new Result(Code.OK, null, "删除成功");
    }

    /**
     * 岗位教学艺术 项目设置
     * 删除
     */
    @DeleteMapping("/delete")
    public Result delete(@RequestBody SkillItemsSet skillItemsSet) {
        SkillItemsSet skillItemsSetOld = iSkillItemsSetService.getById(skillItemsSet.getId());
        if (Objects.isNull(skillItemsSetOld)) {
            return new Result(Code.ERROR, null, "该数据不存在");
        }
        QueryWrapper<SkillStationsItems> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("items_id", skillItemsSet.getId());
        queryWrapper.eq("deleted", 0);
        long count = iSkillStationsItemsService.count(queryWrapper);
        if (count > 0) {
            return new Result(Code.ERROR, null, "该数据有绑定数据,无法删除");
        }
        boolean delete = iSkillItemsSetService.removeById(skillItemsSet);
        if (!delete) {
            return new Result(Code.ERROR, null, "删除失败");
        }
        return new Result(Code.OK, null, "删除成功");
    }

    @GetMapping("/pageList")
    public Result pageList(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "departmentId", required = false) String departmentId,  //27：市场培训，23：教学培训
            @RequestParam(value = "status", required = false, defaultValue = "1") String status,
            @RequestParam(value = "type", required = false) String type
    ) {
        IPage<SkillItemsSet> ipage = new Page<>(pageNum, pageSize);
        QueryWrapper<SkillItemsSet> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("*", "(select d.name from dict d where d.num = skill_items_set.type and d.type = 44 and d.status = 1 and d.deleted = 0) as type_name ");
        queryWrapper.eq(StringUtils.isNotBlank(departmentId), "department_id", departmentId);
        queryWrapper.eq(StringUtils.isNotBlank(type), "type", type);
        queryWrapper.eq(StringUtils.isNotBlank(type), "status", status);
        queryWrapper.orderByAsc("sort");
        ipage = iSkillItemsSetService.page(ipage, queryWrapper);
        return new Result(Code.OK, new PageResult<>(ipage.getRecords(), ipage.getTotal(), ipage.getPages()), Code.GET_OK_MSG);
    }

}
